package com.tgy.binarysearch;

import java.util.Objects;

/**
 * @Author: tgy
 * @Date: 2020-11-17 16:06
 *
 * 二分查找返回value(可能有重复)第一次出现的下标，如无return -1
 */
public class BSFirstEqualValue {

    public int binarySearch(Integer[] nums, int value) {

        if (Objects.isNull(nums) || nums.length == 0) {

            return -1;
        }

        int left = 0, right = nums.length,mid;

        while (left != right) {

            mid = (left + right) >> 1;

            if (value <= nums[mid]) {

                right = mid;
            }else {

                left = mid + 1;
            }

        }

        return nums[left] == value? left:-1;
    }
}
